package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.exception.OrderFullException;
import com.itheima.exception.OrderNotAllowedException;
import com.itheima.exception.OrderRepeatedException;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

/**
 * 套餐预约控制器
 */

@RestController
@RequestMapping("/order")
public class OrderController {
    
    @Reference
    private OrderService orderService;
    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/submitOrder")
    //此时前端传入的为orderInfo的json数据，没有实体类，选择使用map接收
    public Result submitOrder(@RequestBody Map map){
        //判断验证码输入是否正确
        //取出redis中的验证码
        String phoneNumber = (String) map.get("phoneNumber");
        //redis中存储的验证码
        String validateCodeInRedis = jedisPool.getResource().get(phoneNumber + RedisMessageConstant.SENDTYPE_ORDER);
        //输入的验证码
        String validateCode = (String) map.get("validateCode");

        if(validateCodeInRedis == null){
            return new Result(false, MessageConstant.VALIDATE_DISABLED);
        }else if(validateCode!=null && validateCodeInRedis.equals(validateCode)) {
            //设置预约方式
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            map.put("orderStatus", Order.ORDERSTATUS_NO);
            //验证码正确,调用service方法
            try {
                //调用service方法，完成预约
                orderService.order(map);
            } catch (Exception e) {
                //对捕获到的异常进行判断
                //String errorSetmealName = e.getMessage();
                if(e instanceof OrderNotAllowedException){
                    //如果是自定义的未进行预约设置的异常
                    return new Result(false,e.getMessage()+MessageConstant.SELECTED_DATE_CANNOT_ORDER);
                }
                if(e instanceof OrderFullException){
                    return new Result(false,e.getMessage()+MessageConstant.ORDER_FULL);
                }
                if(e instanceof OrderRepeatedException){
                    return new Result(false,e.getMessage()+MessageConstant.HAS_ORDERED);
                }
                //如果不是自定义的异常
                return new Result(false,MessageConstant.ORDER_FAIL);
            }

            //预约成功，给用户发送短信
            try {
                //控制台打印短信
                System.out.println("给"+phoneNumber+"发送短信：预约成功");
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, phoneNumber, "");
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
        return new Result(true,MessageConstant.ORDER_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer orderId){
        //调用service方法，根据id查询预约的详情信息
        try {
            //因为此时查询的数据除了order的基本信息以外，还有预约套餐的信息等，所以使用map封装
            Map map = orderService.findById(orderId);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }

}
